﻿Imports System.Windows.Media.Animation

Partial Public Class CredentialProviderBox
    Implements Microsoft.TeamFoundation.Client.ICredentialsProvider

#Region " ICredentialsProvider "

    Public Delegate Sub EnableDisplayhandler()

    Public Function GetCredentials(ByVal uri As System.Uri, ByVal failedCredentials As System.Net.ICredentials) As System.Net.ICredentials Implements Microsoft.TeamFoundation.Client.ICredentialsProvider.GetCredentials
        m_Uri = uri
        Me.Dispatcher.BeginInvoke(Windows.Threading.DispatcherPriority.Normal, New EnableDisplayhandler(AddressOf EnableDisplay))
        While Not m_SelectedCredentials
            ' Do nothing but wait
        End While
        ' Reset Box
        m_SelectedCredentials = False
        Return m_Credentials
    End Function

    Public Sub NotifyCredentialsAuthenticated(ByVal uri As System.Uri) Implements Microsoft.TeamFoundation.Client.ICredentialsProvider.NotifyCredentialsAuthenticated
        Me.Dispatcher.BeginInvoke(Windows.Threading.DispatcherPriority.Normal, New EnableDisplayhandler(AddressOf Authenticated))

    End Sub

#End Region

    Private m_Uri As System.Uri

    Private m_Credentials As System.Net.ICredentials

    Public ReadOnly Property Credentials() As System.Net.ICredentials
        Get
            Return m_Credentials
        End Get
    End Property

    Private m_SelectedCredentials As Boolean = False

    Private Sub EnableDisplay()
        Me.Visibility = Windows.Visibility.Visible
        Me.Opacity = 1.0
        ServerConnectLabel.Content = String.Format("Conect to {0}", m_Uri.ToString)
        ux_OKButton.IsEnabled = True
        ux_CancelButton.IsEnabled = True
        ux_UsernameTextbox.Focus()
    End Sub

    Private Sub Authenticated()
        Me.ux_UsernameTextbox.Text = ""
        Me.ux_PasswordTextbox.Password = ""
        Me.Opacity = 1.0
        Me.Visibility = Windows.Visibility.Hidden
    End Sub

    Private Sub ux_CancelButton_Click(ByVal sender As System.Object, ByVal e As System.Windows.RoutedEventArgs) Handles ux_CancelButton.Click
        Me.ux_UsernameTextbox.Text = ""
        Me.ux_PasswordTextbox.Password = ""
        Me.Opacity = 1.0
        Me.Visibility = Windows.Visibility.Hidden
    End Sub

    Private Sub ux_OKButton_Click(ByVal sender As System.Object, ByVal e As System.Windows.RoutedEventArgs) Handles ux_OKButton.Click
        If Me.ux_UsernameTextbox.Text = "" Then
            Me.ux_UsernameTextbox.Background = New Windows.Media.SolidColorBrush(Windows.Media.Colors.Red)
            Exit Sub
        Else
            Me.ux_UsernameTextbox.Background = New Windows.Media.SolidColorBrush(Windows.Media.Colors.White)
        End If
        If Me.ux_PasswordTextbox.Password = "" Then
            Me.ux_PasswordTextbox.Background = New Windows.Media.SolidColorBrush(Windows.Media.Colors.Red)
            Exit Sub
        Else
            Me.ux_PasswordTextbox.Background = New Windows.Media.SolidColorBrush(Windows.Media.Colors.White)
        End If
        Dim username As String = Me.ux_UsernameTextbox.Text
        Dim Domain As String = ""
        Dim Password As String = Me.ux_PasswordTextbox.Password
        If Me.ux_UsernameTextbox.Text.IndexOf("\") > 0 Then
            Dim split() As String = Me.ux_UsernameTextbox.Text.Split("\")
            Domain = split(0)
            username = split(1)
        Else
            username = Me.ux_UsernameTextbox.Text
        End If
        Me.Opacity = 0.5
        Me.m_Credentials = New System.Net.NetworkCredential(username, Password, Domain)
        m_SelectedCredentials = True
        ux_OKButton.IsEnabled = False
        ux_CancelButton.IsEnabled = False
    End Sub

End Class
